package pri.lt.client;

import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import pri.lt.Constants;
import pri.lt.bean.BBSListResponse;
import pri.lt.bean.BBSResponse;
import pri.lt.bean.Label;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LabelClient {

    public static String getLabelIdByName(String name) {

        RestTemplate restTemplate = new RestTemplate();
        Map<String, String> params2= new HashMap<>();
        params2.put("name", name);

        ResponseEntity<BBSResponse> result = null;
        try {
            result = restTemplate.getForEntity(Constants.BBS_LABEL_URL + "?name={name}",  BBSResponse.class, params2);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        System.out.println("getLabelIdByName result: " + result.getStatusCode());

        String labelId = result.getBody().getData();
        if (labelId != null && labelId.trim().length() > 0) {
            return labelId;
        }
        return null;
    }

    public static void addLabel(String name, String parentLabelId) {

        RestTemplate restTemplate = new RestTemplate();


        HttpHeaders headers = new HttpHeaders();
//  //请勿轻易改变此提交方式，大部分的情况下，提交方式都是表单提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.add("Cookie", "JSESSIONID=" + UserClient.jsessionid);
//  //封装参数，千万不要替换为Map与HashMap，否则参数无法传递
        MultiValueMap<String, Object> params2= new LinkedMultiValueMap<String, Object>();

//  //也支持中文
        params2.add("name", name);
        if (parentLabelId != null && parentLabelId.trim().length() > 0) {
            params2.add("parentId", parentLabelId.trim());
        }

        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(params2, headers);


        ResponseEntity<BBSResponse> result = restTemplate.exchange(Constants.BBS_LABEL_ADD, HttpMethod.POST, requestEntity, BBSResponse.class, params2);
        System.out.println("addLabel result: " + result.getStatusCode());
    }

    public static List<Label> getAllLabelIds() {
        RestTemplate restTemplate = new RestTemplate();
        Map<String, String> params2= new HashMap<>();

        ResponseEntity<BBSListResponse> result = null;
        try {
            result = restTemplate.getForEntity(Constants.BBS_ALL_LABEL_URL,  BBSListResponse.class, params2);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        System.out.println("getLabelIdByName result: " + result.getStatusCode());
        return result.getBody().getData();
    }

    public static void updateHotContent(String labelId) {
        RestTemplate restTemplate = new RestTemplate();
        MultiValueMap<String, Object> params2= new LinkedMultiValueMap<String, Object>();

        HttpHeaders headers = new HttpHeaders();
//  //请勿轻易改变此提交方式，大部分的情况下，提交方式都是表单提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.add("Cookie", "JSESSIONID=" + UserClient.jsessionid);

        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(params2, headers);


        ResponseEntity<BBSResponse> result = null;
        try {
            result = restTemplate.exchange(Constants.BBS_ADMIN_LABEL_UPDATE_HOT_CONTENT + labelId,  HttpMethod.GET, requestEntity, BBSResponse.class);
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("getLabelIdByName result: " + result.getStatusCode());
    }
}
